package com.bzkj.basicinfo.service.impl;

import com.bzkj.basicinfo.domain.basic.vo.BasicPicInfoVo;
import com.bzkj.basicinfo.mapper.SelectMapper;
import com.bzkj.basicinfo.service.SelectService;
import com.bzkj.common.core.domain.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @projectName: bzkj-wms
 * @package: com.bzkj.basicinfo.service.impl
 * @className: SelectServiceImpl
 * @description: 前端下拉框数据服务层
 * @author: C1309
 * @createDate: 2024年11月20日
 * @version: v1.0
 */
@Service
public class SelectServiceImpl implements SelectService {
    @Resource
    private SelectMapper selectMapper;

    /**
     * 查询仓库信息
     *
     * @return 查询结果
     */
    @Override
    public ArrayList<HashMap<String, Object>> selectStoStock() {
        return selectMapper.selectStoStock();
    }

    /***
     * 查询单据状态信息
     *
     * @return 查询结果
     */
    @Override
    public ArrayList<HashMap<String, Object>> selectStoStatus() {
        return selectMapper.selectStoStatus();
    }

    /**
     * 查询NCC用户信息
     *
     * @param userName 用户名
     * @return 查询结果
     */
    @Override
    public ArrayList<HashMap<String, Object>> selectNccUser(String userName) {
        return selectMapper.selectNccUser(userName);
    }

    /**
     * 查询NCC物料信息
     *
     * @param materialName 物料名称
     * @param modeType     模式类型(0：按名称查 1：按编码查)
     * @return 查询结果
     */
    @Override
    public ArrayList<HashMap<String, Object>> selectNccMaterial(String materialName, int modeType) {
        return selectMapper.selectNccMaterial(materialName, modeType);
    }

    /**
     * 查询NCC部门信息
     *
     * @param manufacturerName 部门名称
     * @return 查询结果
     */
    @Override
    public ArrayList<HashMap<String, Object>> selectNccManufacturer(String manufacturerName) {
        return selectMapper.selectNccManufacturer(manufacturerName);
    }

    /**
     * 查询NCC部门信息
     *
     * @param deptName 部门名称
     * @return 查询结果
     */
    @Override
    public ArrayList<HashMap<String, Object>> selectNccDept(String deptName) {
        return selectMapper.selectNccDept(deptName);
    }

    /**
     * 查询NCC物料编码
     *
     * @param materialName 物料名称
     * @return 查询结果
     */
    @Override
    public ArrayList<HashMap<String, Object>> selectNccMaterialCode(String materialName) {
        return selectMapper.selectNccMaterialCode(materialName);
    }

    /**
     * 根据NCC物料主键查询NCC物料详细信息
     *
     * @param primaryKey 主键
     * @return 查询结果
     */
    @Override
    public HashMap<String, Object> selectNccMaterialByPrimaryKey(String primaryKey) {
        return selectMapper.selectNccMaterialByPrimaryKey(primaryKey);
    }

    /**
     * 根据NCC物料主键、模式类型、批次号查询NCC物料库存信息
     *
     * @param primaryKey 主键
     * @param modeType   模式类型
     * @param batchNo    批次号
     * @return 查询结果
     */
    @Override
    public List<HashMap<String, Object>> selectSkuInfoByPrimaryKey(String primaryKey,
                                                                   String modeType,
                                                                   String batchNo) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<HashMap<String, Object>> queryResult = selectMapper.selectSkuInfoByPrimaryKey(primaryKey, modeType, batchNo);

        for (HashMap<String, Object> queryResultMap : queryResult) {
            // 生产日期
            Object pdDate = queryResultMap.get("pd_date");
            try {
                if (pdDate != null && !pdDate.toString().trim().isEmpty()) {
                    // 假设 pdDate 是一个字符串，先解析为 Date 类型
                    Date parsedPdDate = dateFormat.parse(pdDate.toString());
                    queryResultMap.put("pd_date", dateFormat.format(parsedPdDate));
                }
                // 有效期
                Object expDate = queryResultMap.get("exp_date");
                if (expDate != null && !expDate.toString().trim().isEmpty()) {
                    Date parsedExpDate = dateFormat.parse(expDate.toString());
                    queryResultMap.put("exp_date", dateFormat.format(parsedExpDate));
                }
                // 复验期
                Object retestDate = queryResultMap.get("retest_date");
                if (retestDate != null && !retestDate.toString().trim().isEmpty()) {
                    Date parsedRetestDate = dateFormat.parse(retestDate.toString());
                    queryResultMap.put("retest_date", dateFormat.format(parsedRetestDate));
                }
            } catch (ParseException e) {
                throw new RuntimeException("日期格式错误，请检查日期是否正确！");
            }
        }
        return queryResult;
    }

    /**
     * 根据NCC物料code查询NCC物料详细信息
     *
     * @param code 主键
     * @return 查询结果
     */
    @Override
    public HashMap<String, Object> selectNccMaterialByCode(String code) {
        return selectMapper.selectNccMaterialByCode(code);
    }

    /**
     * 查询NCC供应商信息
     *
     * @param supplierName 供应商名称
     * @return 查询结果
     */
    @Override
    public ArrayList<HashMap<String, Object>> selectNccSupplier(String supplierName) {
        return selectMapper.selectNccSupplier(supplierName);
    }

    /**
     * 查询NCC供应商信息pk
     *
     * @param supplierName 供应商名称
     * @return 查询结果
     */
    @Override
    public ArrayList<HashMap<String, Object>> selectSupplierPk(String supplierName) {
        return selectMapper.selectSupplierPk(supplierName);
    }

    /**
     * 查询TMS系统物流企业信息
     *
     * @param logistics 物流企业名称
     * @return 物流企业信息
     */
    @Override
    public ArrayList<HashMap<String, Object>> selectTmsLogistics(String logistics) {
        return selectMapper.selectTmsLogistics(logistics);
    }

    /**
     * 查询NCC用户主键信息
     *
     * @param username 用户名
     * @return 查询结果
     */
    @Override
    public ArrayList<HashMap<String, Object>> selectNccUserCode(String username) {
        return selectMapper.selectNccUserCode(username);
    }

    /**
     * 查询NCC部门信息(code值)
     *
     * @param deptName 部门名称
     * @return 查询结果
     */
    @Override
    public ArrayList<HashMap<String, Object>> selectNccDeptCode(String deptName) {
        return selectMapper.selectNccDeptCode(deptName);
    }

    /**
     * 查询NCC生产厂商信息pk
     *
     * @param manufacturerName 供应商名称
     * @return 查询结果
     */
    @Override
    public ArrayList<HashMap<String, Object>> selectNccManufacturerPk(String manufacturerName) {
        return selectMapper.selectNccManufacturerPk(manufacturerName);
    }


    @Override
    public ArrayList<HashMap<String, Object>> selectstockAll() {
        return selectMapper.selectstockAll();
    }

    @Override
    public ArrayList<HashMap<String, Object>> selectParentClassId() {
        return selectMapper.selectParentClassId();
    }

    @Override
    public ArrayList<HashMap<String, Object>> selectAreaAll() {
        return selectMapper.selectAreaAll();
    }

    @Override
    public R findAllBasicPicInfo(String type) {
        List<BasicPicInfoVo> list = selectMapper.findAllBasicPicInfo(type);
        return R.ok(list);
    }
}
